package com.example.Interceptor.test;/*
   
/**
* @program: demo
*
* @description: 拦截器demo
*
* @author: kk
*
* @create: 2020-03-20 15:36
**/

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component//作为一个pojo，注入，让spring来管理
public class DemoInterceptor extends HandlerInterceptorAdapter {

    /*
     * @description: 拦截前做一些事
     * @param:
     * @return:
     * @author: keguazi
     * @date: 2020/3/20
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // TODO: 2020/3/20
        System.out.println("这是一个拦截器：开始拦截");
        return true;
    }

    /*
     * @description: 请求结束执行的，但只有preHandle方法返回true的时候才会执行
     * @param:
     * @return:
     * @author: keguazi
     * @date: 2020/3/20
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        // TODO: 2020/3/20
        System.out.println("这是一个拦截器：开始处理");
    }

    /*
     * @description: afterCompletion是视图渲染完成后才执行，同样需要preHandle返回true，该方法通常用于清理资源等工作
     * @param:
     * @return:
     * @author: keguazi
     * @date: 2020/3/20
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("这是一个拦截器：处理完毕");
    }

}
